package com.huike.contract.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ISysUserService;
import com.huike.clues.service.ITbRulePoolService;

import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TransferServiceImpl implements ITransferService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ITbRulePoolService rulePoolService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TbClueMapper clueMapper;





    @Override
    public void selcetinsetassignment(String type,Long userId,Long transferUserId) {
        String createBy = SecurityUtils.getUsername();
        //获得转出用户所有type状态且为最新分配人且是可以分配的数据
        List<TbAssignRecord> listUser = assignRecordMapper.getListByLatestByType(type,userId);
        List<TbAssignRecord> listTransfer = new ArrayList<>();
        for (TbAssignRecord assignRecord : listUser) {
            SysUser sysUser = sysUserMapper.selectUserById(transferUserId);
            TbAssignRecord transfer = new TbAssignRecord();
            transfer.setId(null);
            transfer.setAssignId(assignRecord.getAssignId());
            transfer.setUserId(sysUser.getUserId());
            transfer.setUserName(sysUser.getUserName());
            transfer.setDeptId(sysUser.getDeptId());
            transfer.setCreateTime(new Date());
            transfer.setCreateBy(createBy);
            transfer.setLatest("1");
            transfer.setType(assignRecord.getType());
            listTransfer.add(transfer);
        }
        //将转出人的数据添加到tb_assign_record表中
        listTransfer.forEach( item -> assignRecordMapper.insertAssignRecord(item));
        //修改转出用户的最新分配者状态
        assignRecordMapper.updateLatestByUserIdByTyped(type,userId);
    }

}
